import api from './index'
import request from '@/utils/request'

export function getChatHistory(params) {
  return request.get(api.ChatHistory, {
    params
  })
}

export function getChatHistoryRecently(contactId) {
  return request.get(api.ChatHistoryRecently, {
    params: { contactId }
  })
}

export function getChatList() {
  return request.get(api.ChatList)
}

export function setChatUnread({ contactId, unread }) {
  return request.post(api.ChatUnread, {
    contactId,
    unread
  }, {
    hideloading: true
  })
}

export function deleteChatItem(contactId) {
  return request.delete(api.ChatItem, {
    params: {
      contactId
    },
    hideloading: true
  })
}

export function sendFile(form, onUploadProgress) {
  const formData = new FormData()
  Object.keys(form).forEach(key => {
    formData.append(key, form[key])
  })
  return request.post(api.ChatFile, formData, { onUploadProgress, timeout: 50 * 1000 })
}

export function sendText(form) {
  return request.post(api.ChatText, form, { hideloading: true })
}
